package com.ct.deadlockdetetor.common.pojo;

import com.ct.deadlockdetetor.common.enums.ResultCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {

    /**
     * 响应数据的格式
     * code :    消息状态码
     * message : 消息码的描述
     * data :    数据对象
     */

    private int code;

    private String message;

    private T data;

    // 不需要返回数据对象
    public static <T> CommonResult<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    // 需要返回数据对象
    public static <T> CommonResult<T> ok(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }

    // 标准错误
    public static <T> CommonResult<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }

    // 状态码之外的情况
    public static <T> CommonResult<T> fail(int code, String message) {
        return assembleResult(null, code, message);
    }

    // 操作失败, 特殊情况
    public static <T> CommonResult<T> fail() {
        return assembleResult(null, ResultCode.FAILED);
    }

    /**
     * 标准情况
     * @param data
     * @param resultCode
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> assembleResult(T data, ResultCode resultCode) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(data);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    /**
     * 状态码之外的情况
     * @param code
     * @param message
     * @param data
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> assembleResult(T data, int code, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}